<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!
	// 查询热门动态,按照浏览数+点赞数+评论数排序
	String hot( Input input) {
		Integer uid = input.getInt("uid", -1);
		Integer groupId = input.getInt("group", -1); // 分组id, 默认全部分类问题
		String group = "";
		if (groupId != -1)
			group = " and q.group_id=" + groupId;
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer page = input.getInt("page", 1); // 下拉刷新
		Integer itemCount = input.getInt("item_count", 5); // 上拉加载
		org.hibernate.StatelessSession ss = DBTools.db("basedb").getSf().openStatelessSession();
		org.hibernate.SQLQuery sqlQuery = ss.createSQLQuery(String.format("select t1.id as id, t1.title as title,case when t1.imgs is null then '[]' else t1.imgs end as imgs, t1.uid as uid, t1.content as content, t1.create_time as create_time, t1.aid as aid, t1.group_id as group_id,(t1.count + t2.count) as hot from (select q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.create_time as create_time, q.aid as aid, q.group_id as group_id, count(c.src_id) as count from quest as q, comment as c where q.id=c.src_id and q.status=1 and c.status=1 and q.hospital=%s " + group + " group by c.src_id) as t1,(select q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.create_time as create_time, q.aid as aid, q.group_id as group_id, count(o.oid) as count from quest as q, operate_history as o where q.id=o.oid and q.hospital=%s " + group + " and o.status=1 and q.status=1 group by o.oid) as t2 where t1.id=t2.id order by ( t1.count + t2.count) desc", new Object[]{hospital, hospital}));
		List<Map> questList = sqlQuery.setFirstResult((page - 1) * itemCount).setMaxResults(itemCount).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		for (Map map:questList) {
			map.put("group", map.remove("group_id"));
		}
		completeQuestUserInfo(questList, hospital, uid);
		List<Map> rankCount = ss.createSQLQuery(String.format("select count(*) as size from (select q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.create_time as create_time, q.aid as aid, q.group_id as group_id, count(c.src_id) as count from quest as q, comment as c where q.id=c.src_id and q.status=1 and c.status=1 and q.hospital=%s " + group + " group by c.src_id) as t1,(select q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.create_time as create_time, q.aid as aid, q.group_id as group_id, count(o.oid) as count from quest as q, operate_history as o where q.id=o.oid and q.hospital=%s " + group + " and o.status=1 and q.status=1 group by o.oid) as t2 where t1.id=t2.id order by ( t1.count + t2.count) desc", new Object[]{hospital, hospital})).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		Integer size =  Tools.getInt(isEmpty(rankCount)?"0":(rankCount.get(0).get("size") + ""));
		if (ss != null) 
			ss.close();
		Message msg = new Message(0);
		Map resultMap = new HashMap();
		resultMap.put("rank", questList);
		resultMap.put("current_page", page);
		resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
		resultMap.put("item_count",  itemCount);
		if (isEmpty(questList))
			msg.setErrcodeAndErrmsg(30041, "没有更多了");
		msg.put("data", resultMap);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, hot(input));
%>
